//
//  AppDelegate.m
//  YBar
//
//  Created by Rich on 16/4/19.
//  Copyright © 2016年 Rich. All rights reserved.
//

#import "AppDelegate.h"
#import "RCWelcomeVC.h"
#import "RCTabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self setNetWorkEnvirenment];
    [self registUmengShare];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSessionExpiredNotificationArrived:) name:SessionExpiredNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetRootViewController:) name:ResetRootVCNotification object:nil];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self launchRootViewController];
    [self.window makeKeyAndVisible];
    
    [self changeUserAgent];
    //[self setIconBadgeNumbers];//去除app上的数字
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark  加载根视图

- (void)launchRootViewController {
    
    if([DataCacheManager isHadLauchAd] == NO)
    {
        [DataCacheManager setHadLauchAd:YES];
        RCWelcomeVC *welcomeVC = [[RCWelcomeVC alloc]init];
        self.window.rootViewController = welcomeVC;
        
    }
    else
    {
        
        RCTabBarController* tabBarController = [[RCTabBarController alloc] init];
        self.window.rootViewController = tabBarController;
        
    }
}

#pragma mark - UmengShare（友盟分享）

-(void)registUmengShare{
    
//    [MobClick startWithAppkey:UmengAppKey reportPolicy:BATCH   channelId:@"App Store"];//初始化友盟统计
//    [UMSocialData setAppKey:UmengAppKey];//初始化友盟社会分享；
//    //打开调试log的开关
//    [UMSocialData openLog:YES];
//    
//    NSString *version = [iPhoneTools getCurrentVersion];
//    [MobClick setAppVersion:version];
//    
//    //设置微信AppId、appSecret，分享url
//    [UMSocialWechatHandler setWXAppId:@"wx16c992b4644596e6" appSecret:@"d4624c36b6795d1d99dcf0547af5443d" url:@"http://www.umeng.com/social"];
//    
//    
//    // 打开新浪微博的SSO开关
//    // 将在新浪微博注册的应用appkey、redirectURL替换下面参数，并在info.plist的URL Scheme中相应添加wb+appkey，如"wb3921700954"，详情请参考官方文档。
//    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"2447575201"
//                                         RedirectURL:@"http://www.sharesdk.cn"];
//    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ, UMShareToQzone, UMShareToWechatSession, UMShareToWechatTimeline]];
    
}

#pragma mark -

- (void)changeUserAgent
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *appMark = @"ifangchou";
    
    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *newUagent = [NSString stringWithFormat:@"%@ iOS/%@/%@",secretAgent,appMark,appVersion];
    NSDictionary *dictionary = [[NSDictionary alloc]
                                initWithObjectsAndKeys:newUagent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    
}

#pragma mark - 测试和生产环境

- (void)setNetWorkEnvirenment {
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if (IS_DEFAULT_P) {
        [userDefault setBool:YES forKey:KEY_IS_P];
    }else{
        [userDefault setBool:NO forKey:KEY_IS_P];
    }
    [userDefault synchronize];
}

#pragma mark - noticeAction

- (void)onSessionExpiredNotificationArrived:(NSNotification *)notification{
    
    RCTabBarController *tabBarController = (RCTabBarController*)self.window.rootViewController;
    UINavigationController *theNavigationController = (UINavigationController*)tabBarController.selectedViewController;
    UIViewController *visibleViewController = theNavigationController.visibleViewController;
    
//    if( [visibleViewController isKindOfClass:[FCLoginVC class]] ){
//        //如果visibleViewController已经是FCLoginViewController类型则不做处理
//        return;
//    }
//    
//    FCLoginVC *loginViewController = [[FCLoginVC alloc] init];
//    loginViewController.hidesBottomBarWhenPushed = YES;
//    [theNavigationController pushViewController:loginViewController animated:YES];
    
}

- (void)resetRootViewController:(NSNotification *)notification{
    
    [self launchRootViewController];
    
}

@end
